<?php
class Menu_model extends CI_Model{
    function __construct() {
        parent::__construct();
    }
    
    function getMenu() {
        $this->db->where("menu_status", 0);
        $rs = $this->db->get("main_menu");
        if($rs->num_rows() > 0){
            return $rs;
        }
    }
    
    function getMenuFromID($id){
        if(isset($id)){
            $this->db->where("menu_id",$id);
        }
        $q = $this->db->get("main_menu");
        if($q->num_rows() > 0 ){
            return $q;
        }
        return $q;
    }
    
    public function GetSearchMenu($stt = NULL, $key = NULL, $limit, $offset) {
        $q = $this->db->select('menu_id,menu_name,menu_status')
                ->from('main_menu')
                ->limit($limit, $offset);

        if ($stt != NULL) {
            $q->where("menu_status", $stt);
        }
        if ($key != NULL) {
            $q->like('menu_name', $key);
        }
        $ret["rows"] = $q->get();

        $q = $this->db->select('count(*) as count', FALSE)->from("main_menu");
        if ($stt != NULL) {
            $q->where("menu_status", $stt);
        }
        if ($key != NULL) {
            $q->like('menu_name', $key);
        }
        $tmp = $q->get()->result();
        $ret["num_rows"] = $tmp[0]->count;
        return $ret;
    }
    
    public function DeleteMenu($mid) {
        $this->db->where("menu_id", $mid);
        $q = $this->db->delete("main_menu");
        if ($q) {
            return TRUE;
        }else
            return FALSE;
    }
    //them menu
    public function addMenu($data) {
        $array = array(
            "menu_name" => $data["txtName"],
            "m_slug" => $data["slug"]
        );

        $this->db->set($array);
        $q = $this->db->insert('main_menu');
        if ($q) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    //sua menu
    public function UpdateMenu($query, $pid) {
        $data = array(
            'menu_name' => $query["txtName"],
            "m_slug" => $query["slug"],
            'menu_status' => $query["CatStatus"]
        );
        if ($pid) {
            $this->db->where("menu_id", $pid);
        }
        $rs = $this->db->update("main_menu", $data);
        if ($rs)
            return TRUE;
        else
            return FALSE;
    }
}
?>
